package com.jm.springboot21.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jm.springboot21.mapper.DeptMapper;
import com.jm.springboot21.pojo.Dept;
import com.jm.springboot21.pojo.PageBean;
import com.jm.springboot21.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public Dept findByName(String name) {
        return deptMapper.findByName(name);
    }


    @Override
    public void addDept(Dept dept) {
        deptMapper.add(dept);
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void updateDept(Dept dept) {
        deptMapper.updateById(dept);
    }

    @Override
    public PageBean getDeptByPage(Integer page, Integer pageSize) {
        // 使用PageHelper设置分页参数
        PageHelper.startPage(page, pageSize);

        // 查询所有数据，此时查询会被自动分页
        List<Dept> deptList = deptMapper.selectAllDepts();

        // 将查询结果转换为PageInfo对象，可以获取更多分页信息
        PageInfo<Dept> pageInfo = new PageInfo<>(deptList);

        // 构建返回的分页结果对象
        PageBean pageBean = new PageBean();
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setRows(pageInfo.getList());

        return pageBean;
    }

    @Override
    public Integer count() {
        return deptMapper.count();
    }
}
